package com.xoms.admin.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.admin.model.ProBreakageBean;
import com.xoms.admin.model.ProBreakageDetailBean;
import com.xoms.admin.model.vo.AuditBreakageBean;
import com.xoms.admin.model.vo.BatchBreakageBean;
import com.xoms.admin.model.vo.ProBreakageVo;
import com.xoms.admin.persist.dao.ProBreakageDao;
import com.xoms.util.StringUtil;

@Repository
public class ProBreakageImpl extends BaseImpl implements ProBreakageDao {
	
	final String NAME_SPACE = NAME_SPACE_HEADER + ".ProBreakageMapper";
	
	@Override
	public int selectCount(String startTime, String endTime, String storeId) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		if (!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		if (!StringUtil.isEmpty(storeId)) {
			params.put("storeHouseId", storeId);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount", params);

	}

	@Override
	public List<ProBreakageBean> selectList(int page, int pageSize, String startTime, String endTime,
			String storeHouseId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		if (!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		if (!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		if (!StringUtil.isEmpty(storeHouseId)) {
			params.put("storeHouseId", storeHouseId);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);

	}

	@Override
	public ProBreakageBean selectOneByStockId(String sid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneByStockId", sid);
	}
	

	@Override
	public String selectMaxNoInYear() {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectMaxNoInYear");
	}

	@Override
	public int replace(ProBreakageBean br) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".replace", br);
	}

	@Override
	public int replaceBatchDetail(List<BatchBreakageBean> breaks) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("list", breaks);
		return sqlSessionTemplate.insert(NAME_SPACE + ".replaceBatchDetail", params);
	
	}

	@Override
	public int selectDetailCount(String bid) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(bid)) {
			params.put("bid", bid);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDetailCount", params);
	
	}

	@Override
	public List<ProBreakageDetailBean> selectDetailList(String bid, int page, int pageSize, int paging) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		if (!StringUtil.isEmpty(bid)) {
			params.put("bid", bid);
		}
		params.put("paging", paging);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDetailList", params);

	}

	@Override
	public int insert(ProBreakageBean data) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", data);
	}

	@Override
	public int update(ProBreakageBean data) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", data);
	}

	@Override
	public ProBreakageBean selectOneById(String bid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", bid);
	}

	@Override
	public int delDetails(String bid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delDetails", bid);
	}

	@Override
	public int del(String bid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delete", bid);
	}

	@Override
	public int updateSubmitStatus(String bid, int status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("bid", bid);
		params.put("status", status);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateSubmitStatus", params);
	}

	@Override
	public AuditBreakageBean selectAuditBreakagesById(String aid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectAuditBreakagesById", aid);
	}

	@Override
	public List<ProBreakageVo> selectProductListByBid(String bid) {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectProductListByBid", bid);
	}


}
